package cn.tedu.alipay.payMainDemo;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;

import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.json.JSONObject;


public class alipayDemo2Main {

    private  static final String appID="2021000120618018";
    private static  final String PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCgLGLeXh1dVPCQw8VCGy/1SfOkBFtZxACS81NkmtTX4DGVxVeDb361QHDaIjzk30Oz0vKZ8WCFl+w/IOr1Rz6HODQJFliu3dak+On+cwzeu+dx5nelmsGlYHVptgiblAp6wBZaF+6RvF5GFIar/VGEfswMsRFC20jG7LaY1kUSpdnESlAjdTg/rfbbZROEELbVfklvvbJQBz2rgKgAAhw5fxXkMsbeJbVN8ZZO1tlSSm/22VmRs3UJbnqnyIwzckgXYyXn5x2Oujjvu1DAFnOeL0e6vkzDuj9lJH+8BI0D44P/MXcFmhifLAMOISNcQCk5FVes2YHI6Cyjw7wT34qhAgMBAAECggEAWpwBai3viqbMV/iHlHfwz2AAVh6o1sUMVhUUbesw0NZg9ormkNH36e0EakHIFOlAUW/aEb/8jFZ/LM8gW5TsCG6A8UsU7AdOr5CoPmszMqU1rrdjo+lB9p30jdKOWASNURdtDZZQPV1EL3zZGl55SOy53cwlKZ8HMpEiUp+T/5GxbRC0Plq1wuoAV+G/XpfDQ7iG1Cc4/laV8odIOvDkVdu6E0L1IQzixsovzi5BnRGIJY9gK8l6tQu8X/ol+iTF5iOuFXRR4HsOwXrKhqOkJbgJkd+m3FpBWWRDhnb1j/Lto91Poef5wBqzjzRCS5il9cYlc9U9QEodBY9QajVNaQKBgQDXfCq7q/O5G9SlXXVoQ2ngTXiItO3YyOHEGhq1bPmivspcTJBDQqyzBqbZBGh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UPMxA7bIeooFhOgL/xYXdvn1qDtO2dV2NsHTeW0/gS29wcUm9m8w9pF5VcjLsi82LaY+ur9XW1DO9U/W87d3tuXd7seSYMw718IgTIOSJ+1tX4/NVUov6bDPCrsBATltcGtN6eM4iH6ix3+ZocSp9DfJQIDAQAB";



    public static void main(String[] args) {

        // 1 实例化客户端
        AlipayClient alipayClient = getAlipayClient();

        //2 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify
        AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        //此次只是参数展示，未进行字符串转义，实际情况下请转义\




        request.setNotifyUrl("");
        request.setReturnUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101004");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
//bizContent.put("time_expire", "2022-08-01 22:00:00");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());




        try{
            AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request);
         //   AlipayTradePagePayResponse response = alipayClient.Execute(request);
            //调用成功，则处理业务逻辑
            if (response.isSuccess()) {
                System.out.println("调用成功。");
            } else {
                System.out.println("调用失败，原因：" + response.getMsg() + "，" + response.getSubMsg());
            }
        } catch (Exception e) {
            System.out.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }

    }

    private static AlipayClient getAlipayClient() {
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        //设置应用ID
        alipayConfig.setAppId(appID);

        //设置应用私钥
        alipayConfig.setPrivateKey(PRIVATE_KEY);
        //设置请求格式，固定值json
        alipayConfig.setFormat("json");
        //设置字符集
        alipayConfig.setCharset("utf-8");
        //设置签名类型
        alipayConfig.setSignType("RSA2");
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(ALIPAY_PUBLIC_KEY);
        //实例化客户端
        AlipayClient alipayClient=null;
        try {
            alipayClient  = new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return alipayClient;
    }
}
